﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using XukaShop.Models.Entities;

namespace XukaShop.Dao
{
    public class PromotionDao
    {
        private FashionShopEntities _db = new FashionShopEntities();

        /// <summary>
        /// Get List Promotion in Database
        /// </summary>
        /// <returns></returns>
        public List<Promotion> GetListPromotions()
        {
            var selectPromotions = (from promotion in _db.Promotion
                                    where promotion.IsDelete == false
                                    select promotion).ToList();
            return selectPromotions;
        }

        /// <summary>
        /// Get a Promotion by Id
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public Promotion GetPromotionById(int promotionId)
        {
            var selectPromotion = (from promotion in _db.Promotion
                                   where promotion.Id == promotionId
                                     && promotion.IsDelete == false
                                   select promotion).FirstOrDefault();
            return selectPromotion;
        }

        /// <summary>
        /// Get a Promotion by Name
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public Promotion GetPromotionByName(string promotionName)
        {
            var selectPromotion = (from promotion in _db.Promotion
                                   where promotion.Name == promotionName
                                     && promotion.IsDelete == false
                                   select promotion).FirstOrDefault();
            return selectPromotion;
        }

        /// <summary>
        /// Get list status of promotion
        /// </summary>
        /// <returns></returns>
        public List<PromotionStatus> GetListPromotionStatus()
        {
            var selectPromotionStatus = (from promotionStatus in _db.PromotionStatus
                                         select promotionStatus).ToList();
            return selectPromotionStatus;
        }

        /// <summary>
        /// Edit a Promotion
        /// </summary>
        /// <param name="brandModel"></param>
        /// <returns></returns>
        public bool EditPromotion(Promotion promotionModel)
        {
            _db.Promotion.Attach(promotionModel);
            _db.Entry(promotionModel).State = EntityState.Modified;
            var queryResult = _db.SaveChanges();
            if (queryResult > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Create a Promotion
        /// </summary>
        /// <param name="brandModel"></param>
        /// <returns></returns>
        public bool CreatePromotion(Promotion promotionModel)
        {
            _db.Promotion.Add(promotionModel);
            var queryResult = _db.SaveChanges();
            if (queryResult > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Delete a Promotion
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns>Id of Promotion</returns>
        public bool DeletePromotion(int promotionId)
        {
            Promotion promotionModel = new Promotion();
            promotionModel = (from promotion in _db.Promotion
                              where promotion.Id == promotionId
                              select promotion).FirstOrDefault();
            if (promotionModel != null)
            {
                promotionModel.IsDelete = true;
                _db.Promotion.Attach(promotionModel);
                _db.Entry(promotionModel).State = EntityState.Modified;
                var queryResult = _db.SaveChanges();
                if (queryResult > 0)
                {
                    return true;
                }
                return false;
            }
            return false;
        }

        /// <summary>
        /// Check Promotion is existed nor not
        /// </summary>
        /// <param name="brandName"></param>
        /// <param name="isUpdateMode">Check when Update or Create</param>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public bool IsExistedPromotion(string promotionName, bool isUpdateMode, int promotionId = 0)
        {
            if (isUpdateMode)
            {
                return _db.Promotion.Any(model => model.Name == promotionName && model.Id != promotionId);
            }
            return _db.Promotion.Any(model => model.Name == promotionName);
        }
    }
}